package com.online_video.service.impl;

import com.online_video.dao.VideoDao;
import com.online_video.dao.impl.VideoDaoImpl;
import com.online_video.entity.*;
import com.online_video.service.VideoService;

import java.util.List;

public class VideoServiceImpl implements VideoService {
    VideoDao videoDao = new VideoDaoImpl();

    @Override
    public int insert(Video video) {
        return videoDao.insert(video);
    }

    @Override
    public int update(Video video) {
        return 0;
    }

    @Override
    public int delete(Integer vid) {
        return videoDao.delete(vid);
    }

    @Override
    public List<Video> queryByViName(String viName) {
        return videoDao.queryByViName(viName);
    }

    @Override
    public List<Video> queryByCategory(String category) {
        List<Video> videoList = videoDao.queryByCategory(category);
        return videoList;
    }
    /*由视频id进行查询*/
    @Override
    public Video queryByVid(Integer vid) {
        Video video = videoDao.queryByVid(vid);
        return video;
    }
    /*由用户的信息进行查询*/
    @Override
    public List<Video> queryByU_id(Integer u_id) {
        return videoDao.queryByU_id(u_id);
    }

    @Override
    public List<CollectBook> queryCollectById(Integer id) {
        List<CollectBook> collectBooks = videoDao.queryCollectBookByid(id);
        return collectBooks;
    }

    @Override
    public boolean isExitCollectBook(Integer u_id, String c_book_name) {
        CollectBook isExitCBook = videoDao.isExitCollectBookById(u_id, c_book_name);
        if (isExitCBook != null){
            return true;
        }
        return false;
    }

    @Override
    public int insertBookCollect(CollectBook collectBook) {
        int result = videoDao.insertCollectBook(collectBook);
        return result;
    }

    @Override
    public List<CollectBook> queryCollect(Integer u_id, Integer c_book_id) {
        List<CollectBook> collectBooks = videoDao.queryCollBook(u_id, c_book_id);
        return collectBooks;
    }

    @Override
    public int insertCollect(Collect collect) {
        int result = videoDao.insertCollect(collect);
        return result;
    }

    @Override
    public int delCollectVide(Integer collectid) {
        int result = videoDao.delCollectVideo(collectid);
        return result;
    }

    @Override
    public int isExitCollectVideo(Integer vid, Integer u_id) {
        List<Collect> isExitCollectVideoList = videoDao.isExitCollectVideo(vid, u_id);
        if (isExitCollectVideoList.size() > 0){
            return 1;
        }
        return -1;
    }

    @Override
    public int cancelCollectVideo(Integer vid, Integer u_id) {
        int result = videoDao.cancelCollectVideo(vid, u_id);
        return result;
    }

    @Override
    public int countCollect(Integer vid) {
        int result = videoDao.collectCount(vid);
        return result;
    }

    @Override
    public VideoPage<Video> page(int pageNo, int pageSize, String parm,String category) {
        VideoPage<Video> page = new VideoPage<>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = videoDao.queryForPageTotalCount(parm,category);
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        int pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0){
            pageTotal++;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        //设置该 page 对象中封装的记录集合
        List<Video> videoList = videoDao.queryForPageItems((page.getPageNo() - 1) * pageSize, pageSize, parm,category);
        page.setItems(videoList);

        return page;
    }

    @Override
    public User queryUserByVid(Integer vid) {
        User user = videoDao.queryUserByVid(vid);
        return user;
    }
}
